package com.controller;

import com.bean.entity.ParentsInfo;
import com.bean.entity.StudentInfo;
import com.bean.entity.Teachers;
import com.bean.pojo.Dto;
import com.bean.util.DtoUtil;
import com.bean.vo.ParentsInfoVo;
import com.bean.vo.StudentInfoVo;
import com.bean.vo.TeachersVo;
import com.service.ParentsInfoService;
import com.service.StudentInfoService;
import com.service.TeachersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
//@RequestMapping("login")
public class LoginController {
    @Autowired
    private ParentsInfoService parentsInfoService;
    @Autowired
    private TeachersService teachersService;
    @Autowired
    private StudentInfoService studentInfoService;

    @RequestMapping("login")
    public Dto login(String userName, String password, HttpSession session){
        System.out.println("用户输入的账号"+userName+"用户输入的密码"+password);

        try {
            List<StudentInfoVo> studentLogin = studentInfoService.query(new StudentInfo(Long.valueOf(userName), password));
            if (studentLogin.size()==1){
                System.out.println("学生登录成功");
                session.setAttribute("studentLoginInfo",studentLogin.get(0).getId());
                return DtoUtil.returnSuccess("student");
            }else {
                List<TeachersVo> teachersLogin = teachersService.query(new Teachers(userName, password));
                if(teachersLogin.size()==1){
                    System.out.println("教师登录成功");
                    session.setAttribute("loginTeacherId",teachersLogin.get(0).getId());
                    if (teachersLogin.get(0).getRoleId()>=6){
                        System.out.println("管理员登录成功");
                        return DtoUtil.returnSuccess("admin");
                    }else return DtoUtil.returnSuccess("teacher");
                }else {
                    List<ParentsInfoVo> parentLogin = parentsInfoService.query(new ParentsInfo(userName, password));
                    if (parentLogin.size()==1){
                        System.out.println("家长登录成功");
                        session.setAttribute("parentsLoginInfo",parentLogin.get(0));
                        return DtoUtil.returnSuccess("parents");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage());
        }
        return null;
    }
}
